/**
 * infer 一般用来做类型推断
 */

/**
 * 从一个字符串中提取名字,Hello,xxx
 */

type HeyiUserName='Hello, heyi';
type ExtractName<T>=T extends `Hello, ${infer Name}`?Name:never;

let heyiName:ExtractName<HeyiUserName>;

/**
 * 从一个函数中提取参数类型和返回值类型
 */
const say=(a:number)=>`${a+1}`;

type ExtractParamsAndReturn<T>=T extends (a:infer Params)=>infer Return?[Params,Return]:never;

let sayType:ExtractParamsAndReturn<typeof say>;

// 条件类型与extends的综合应用
type MyType<T> = T extends Function?"It's a function":"It's not a function";

type Result1=MyType<()=>void>; //是一个函数
type Result2=MyType<string>; //这不是一个函数

type ReturnTypeOf<T>=T extends (...args:any[])=>infer R?R:never;

function greet(name:string):string{
    return `Hello,${name}`;
}

type GreetReturnType=ReturnTypeOf<typeof greet>; //GreetReturnType 会被推导为string

type ResolvedType<T>=T extends Promise<infer R>?R:T;

type ResolvedString=ResolvedType<Promise<string>>; //推断的解析类型是string
type ResolvedNumber=ResolvedType<Promise<number>>; //解析为number类型

console.log("hi")

// 从联合类型中提取特定类型
type ExtractType<T, U> = T extends U ? T : never;

type MyUnion = string | number | boolean;
type StringOrNumber = ExtractType<MyUnion, string | number>; // string | number
